<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Books extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->user_auth->manage();

		$this->load->model('category_model');
		$this->load->model('book_model');

		$this->menu_view = $this->menu_lib->get(2);

		$this->data = array();
		$this->data['categories'] = $this->category_model->get()->result();
		$this->data['categories_id'] = $this->input->get('categories_id');
	}

	public function index()
	{
		//$data = array(
		//	'content' => $this->flat_app->get('index.php')
		//);
		//$this->load->view('template', $data);
		$action = $this->input->get('action');

		switch($action)
		{
		case 'create':
			if(!empty($_GET['name'])&&isset($_GET['categories_id']))
			{
				$res = $this->book_model->add($_GET);

				if($res)
				{
					$this->data['text_success'] = 'Libro agregado';
				}
				else
				{
					$this->data['text_error'] = 'Error al tratar de agregar libro';
				}
			}
			break;
		case 'delete':
			$res = $this->book_model->delete(array('id' => $_GET['id']));
			if($res)
			{
				$this->data['text_success'] = 'Libro eliminado';
			}
			else
			{
				$this->data['text_error'] = 'Error al tratar de eliminar libro';
			}
			break;
		case 'update':
			$res = $this->book_model->update($_GET);
			if($res)
			{
				$this->data['text_success'] = 'Libro actualizado';
			}
			else
			{
				$this->data ['text_error'] = 'Error al tratar de actualizar libro';
			}
			break;
		}

		$this->data['books'] = $this->book_model->get(!empty($this->data['categories_id']) ? $this->data : array())->result();
		$include_view=$this->load->view('books', $this->data, TRUE);
		$this->load->view('template', array('content' => $this->menu_view . $include_view));
	}

	public function add()
	{
		//$_GET['submodule'] = 'new';
		//$this->index();
		$include_view=$this->load->view('books_new', $this->data, TRUE);
		$this->load->view('template', array('content' => $this->menu_view . $include_view));
	}

	public function edit()
	{
		//$this->index();
		$this->data['row'] = $this->book_model->get(array('id' => $this->input->get('id')));

		if(empty($this->data['row']))
		{
			$include_view=$this->load->view('books_404', array(), TRUE);
		}
		else
		{
			$include_view=$this->load->view('books_edit', $this->data, TRUE);
		}
		$this->load->view('template', array('content' => $this->menu_view . $include_view));
	}

}

/* End of file books.php */
/* Location: ./application/controllers/books.php */
